package objects;
/**
 * Klasa Lini
 * @author Roman Barzyczak
 *
 */
public class Line {
//		/**
//		 * Precyzja roznicy pomiedzy obliczonym y a y punktu
//		 */
//		private static final int PRECISION = 15;
//		/**
//		 * Precyzja, kiedy linia jest zaliczona jaka prosta
//		 */
//		private static final int PRECISIONFORVERTICALLINE = 10;
		/**
		 * Precyzja, kiedy dwie linie sa rowne
		 */
		private final static int LINEPRECISION = 10;
		/**
		 * Precyzja, kiedy dwie linie sa rowne
		 */
		private final static int DIAGONALPRECISION = 9;
		/**
		 * Wspolrzedne punktow
		 */
		int x1,x2,y1,y2;
		/**
		 * wspolczynniki linii a i b
		 */
		double a,b;
		
		Point p1,p2;
		public Line (Point p1, Point p2) {
			this.x1 =p1.x;
			this.y1 = p1.y;
			this.x2 = p2.x;
			this.y2 = p2.y;
			this.p1 = p1;
			this.p2 = p2;
			if ((x2-x1) != 0) {
				this.a = calcA();
				this.b = calcB();
			} else if(Math.abs(x2 - x1) < this.getLength()/5) {
				this.a = 0;
				this.b = x1;
			}
		}

		/**
		 * Sprawdzenie czy podany punkt znajduje sie linii
		 * @param point
		 * @return
		 */
		public boolean isPointOnLine(Point point) 
		{
			int xP = point.x; 
			int yP = point.y;
			if(Math.abs(x2 - x1) < this.getLength()/5 ) {
					int maxY = (y1 >=y2 ? y1 : y2);
					int minY = (y1 < y2 ? y1 : y2);
					if(yP <= maxY && yP >= minY) {

						return true;
					} else {
						return false;
					}
			} else 	if(Math.abs(y2 - y1) < this.getLength()/5 ) {
				int maxX = (x1 >=x2 ? x1 : x2);
				int minX = (x1 < x2 ? x1 : x2);
				if(xP <= maxX && xP >= minX) {
	
					return true;
				} else {
//
//					System.out.println("max = " +maxX + " y="+ minX);
//					
//					System.out.println(" x p = " +xP + " y="+ yP);
					return false;

				}
			}	else {
				double a = getA();
				double b = getB();
				long y = Math.round((a*xP+b));
				if ( Math.abs(y-yP) <= this.getLength()/5 ){
				       return true;
				} else {
//					System.out.println("y  " + y);
//					System.out.println("isonline x = " +xP + " y="+ yP);
//					
//					System.out.println("isonline x = " +xP + " y="+ yP);
					return false;
				}
			}

		
		}
		
		private double calcA() {
			double dy = y2-y1;
			double dx = x2-x1;
			double a = dy/dx;
			return a;
		}
		
		private double calcB() {
			double dy =  y2-y1;
			double dx = x2-x1;
			double b = -((dy)/(dx))*x1+y1;
			return b;
		}
		
		public double getA() {
			return a;
		}
		
		public double getB() {
			return b;
		}
		
		/**
		 * Obliczenie dlugosci
		 * @param l1 - linia
		 * @return length
		 */
		public double getLength() {
			double dx = Math.abs(x2 - x1);
			double dy = Math.abs(y2 - y1);
			double length = Math.sqrt(Math.pow((double)dx, 2) + Math.pow((double)dy, 2));
			return length;
		}
		
		/**
		 * Sprawdzenie czy linie sa rowne, biorac pod uwage precyzje bledu
		 * @param length1
		 * @param length2
		 * @return
		 */
		public static boolean lineLengthIsEqual(double length1, double length2) {
			double value = length2-length1;
			if(Math.abs(value) <= LINEPRECISION) {
				return true;
			}
		//	System.out.println("wartpsc " + value);
			return false;
		}
		
		/**
		 * Sprawdzenie czy przekatne sa rownie, biorac pod uwage precyzje bledu
		 * @param length1
		 * @param length2
		 * @return
		 */
		public static boolean diagonalIsEqual(double length1, double length2) {
			double value = length2-length1;
			if(Math.abs(value) <= DIAGONALPRECISION) {
			//	System.out.println("wartpsc " + value);
				return true;
			}
	//		System.out.println("wartpsc diag " + value);
			return false;
		}
}
